package com.kedou.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;

import com.bytedance.applog.AppLog;
import com.bytedance.applog.ILogger;
import com.bytedance.applog.InitConfig;
import com.bytedance.applog.game.GameReportHelper;
import com.bytedance.applog.util.UriConstants;
import com.bytedance.hume.readapk.HumeSDK;
import com.github.gzuliyujiang.oaid.DeviceID;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;
import com.kedou.sdk.assist.CallbackObservable;
import com.kedou.sdk.assist.Constant;
import com.kedou.sdk.assist.Constant.LocalIntentExtra;
import com.kedou.sdk.content.HNSSNAbsActivity;
import com.kedou.sdk.content.HNSSNActivityListener;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.core.HttpEngineConduits;
import com.kedou.sdk.download.ActivitiesDownloader;
import com.kedou.sdk.download.VersionUpdateDownloader;
import com.kedou.sdk.handler.SimpleAsyncResponseHandler;
import com.kedou.sdk.params.AgreementResponseParams;
import com.kedou.sdk.params.ConfigInfoResponseParams;
import com.kedou.sdk.params.RoleInfoResponseParams;
import com.kedou.sdk.params.bean.ConfigInfo;
import com.kedou.sdk.params.bean.ExitPopup;
import com.kedou.sdk.params.bean.Goods;
import com.kedou.sdk.params.bean.NavModel;
import com.kedou.sdk.params.bean.RealNameInfo;
import com.kedou.sdk.params.bean.RoleInfo;
import com.kedou.sdk.params.bean.RoleStatus;
import com.kedou.sdk.ui.activity.PrivateActivity;
import com.kedou.sdk.utils.ToolsUtils;
import com.kedou.sdk.utils.WL;
import com.kedou.sdk.utils.XmlUtils;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/27 0027.
 */

public final class JuHaoWanSDK implements HNSSNActivityListener {

    private static final String TAG = JuHaoWanSDK.class.getSimpleName();

    private volatile static JuHaoWanSDK instance;

    private JuHaoWanSDKConfiguration configuration;
    private HNSSNActivityListener hnssnProxyActivity;

    private volatile RoleInfo mRoleInfo;
    private Context applicationContent;
    private Activity mActivity;


    public RoleInfo getmRoleInfo() {
        return mRoleInfo;
    }

    private JuHaoWanSDK() {
    }

    public static JuHaoWanSDK getInstance() {
        if (instance == null) {
            synchronized (JuHaoWanSDK.class) {
                if (instance == null) {
                    instance = new JuHaoWanSDK();
                }
            }
        }
        return instance;
    }

    private static void destroyInstance() {
        instance = null;
    }

    public Context getApplicationContent() {
        return applicationContent;
    }


    private void initOaid(){
        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
        String thirdOaid = conduits.readCurrentOaid();
        if(TextUtils.isEmpty(thirdOaid)){
            if(DeviceID.supportedOAID(configuration.getApplicationContext())){
                String temp =DeviceID.getOAID();
                thirdOaid = TextUtils.isEmpty(temp)?DeviceID.getOAID():temp;
            }else {
                thirdOaid = "empty";
            }
            conduits.saveCurrentOaid(thirdOaid);
        }
    }

    public synchronized void init(JuHaoWanSDKConfiguration configuration) {

        if (configuration == null) {

            throw new IllegalArgumentException("WanSDK configuration can not be initialized with null");
        }
        //parse config xml
        if (!XmlUtils.isInitFinished()) {
            XmlUtils.parseConfigValue(configuration.getApplicationContext());
        }

        this.applicationContent = configuration.getApplicationContext();
        if (this.configuration == null) {
            this.configuration = configuration;
        } else {
            WL.w(TAG, "Try to initialize WanSDK which had already been initialized before. To re-init WanSDK with new configuration call WanSDK.destroy() at first.");
        }

        String className = XmlUtils.getSdkHNSSNProxyActivityClass();
        Object object = ToolsUtils.createInstance(className);
        if (object instanceof HNSSNActivityListener) {
            hnssnProxyActivity = (HNSSNActivityListener) object;
        }

        VersionUpdateDownloader.getImpl().init(configuration.getApplicationContext());
        ActivitiesDownloader.getImpl().init(configuration.getApplicationContext());

        HttpEngineApi.getInstance().init(configuration);

//        HttpEngineApi.getInstance().checkVersion(new SimpleAsyncResponseHandler<CheckVersionResponseParams>(CheckVersionResponseParams.class) {
//            @Override
//            public void onAsyncResponseSuccess(int flag, CheckVersionResponseParams response) {
//                if(response != null && response.isSuccess()){
//                    HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//                    conduits.cacheVersionInfo(response.getData());
//                }
//            }
//        });

    }

    private boolean isToutiaoInit = false;
    public boolean isToutiaoInit(){
        return isToutiaoInit;
    }
//    private static void toutiaoInit(Context con,int toutiaoID,Activity act){
//        Log.e("toutiao","toutiaoid"+toutiaoID);
//        final InitConfig config = new InitConfig(String.valueOf(toutiaoID), "default");
//        config.setUriConfig(UriConstants.DEFAULT);
//        AppLog.setEncryptAndCompress(false);
//        config.setAutoTrackEnabled(true);
//        config.setLogger(new ILogger() {
//            @Override
//            public void log(String s, Throwable throwable) {
//                Log.e("toutiao",s);
//            }
//        });
//        config.setEnablePlay(true);
//
//        config.setAbEnable(true); // 是否开启A/B Test功能
//        config.setAutoStart(true);
//        AppLog.init(con, config,act);
//
//    }





    public synchronized void initToutiao(){
        String toutiaoID = configuration.getToutiaoId();
        Log.e("toutiao","toutiaoid"+toutiaoID);
        if(TextUtils.isEmpty(toutiaoID)||"0".equals(toutiaoID)){
            return;
        }
        final InitConfig config = new InitConfig(toutiaoID, "default");
        config.setUriConfig(UriConstants.DEFAULT);
        AppLog.setEncryptAndCompress(false);
        config.setAutoTrackEnabled(true);
        config.setLogger(new ILogger() {
            @Override
            public void log(String s, Throwable throwable) {
                Log.e("toutiao",s);
            }
        });
        config.setEnablePlay(true);

        config.setAbEnable(true); // 是否开启A/B Test功能
        config.setAutoStart(true);
        AppLog.init(configuration.getApplicationContext(), config);
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                final InitConfig config = new InitConfig(toutiaoID, "default");
//                config.setUriConfig(UriConstants.DEFAULT);
//                AppLog.setEncryptAndCompress(false);
//                config.setAutoTrackEnabled(true);
//                config.setLogger(new ILogger() {
//                    @Override
//                    public void log(String s, Throwable throwable) {
//                        Log.e("toutiao",s);
//                    }
//                });
//                config.setEnablePlay(true);
//
//                config.setAbEnable(true); // 是否开启A/B Test功能
//                config.setAutoStart(true);
//                AppLog.init(configuration.getApplicationContext(), config,activity);
//            }
//        });
        isToutiaoInit = true;
    }

    private HashMap<String,Bitmap> menuBitmap=new HashMap<>();
    public  HashMap<String,Bitmap> getMenuBitmap(){
        return menuBitmap;
    }
        private void loadMenu(List<NavModel> models){
            for (NavModel mo:models) {
                Picasso.with(getApplicationContent()).load(mo.getIcon_select()).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                        Matrix matrix = new Matrix();
                        matrix.postScale(2, 2);
                        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                        menuBitmap.put(mo.getName()+"select",resizedBitmap);
                    }

                    @Override
                    public void onBitmapFailed(Drawable drawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable drawable) {

                    }
                });
                Picasso.with(getApplicationContent()).load(mo.getIcon_nchecked()).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                        Matrix matrix = new Matrix();
                        matrix.postScale(1.2f, 1.2f);
                        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                        menuBitmap.put(mo.getName(),resizedBitmap);
                    }

                    @Override
                    public void onBitmapFailed(Drawable drawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable drawable) {

                    }
                });
            }
        }

    private void realInit(JuHaoWanSDKConfiguration configuration, final JuHaoWanSDKCallback callback){
        try {
            DeviceIdentifier.register((Application) configuration.getApplicationContext());
        }catch (Exception e){
            e.printStackTrace();
        }

        initOaid();
        HttpEngineApi.getInstance().getPlatformConfig(new SimpleAsyncResponseHandler<ConfigInfoResponseParams>(ConfigInfoResponseParams.class) {
            @Override
            public void onAsyncResponseSuccess(int flag, ConfigInfoResponseParams response) {
                if (response != null && response.isSuccess()) {
                    HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
                    conduits.setPlatformConfigInfo(response.getData());
                    loadMenu(response.getData().getNav_list());

                    initToutiao();

                    if (callback != null) {
                        String message = response.getMsg();
                        callback.onWanSDKResponse(Constant.InitState.INIT_SUCCESS, message, null);
                    }
                } else {
                    HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                    if (callback != null) {
                        String message = response != null ? response.getMsg() : "init failed";
                        callback.onWanSDKResponse(Constant.InitState.INIT_FAILED, message, null);
                    }
                }
            }

            @Override
            public void onAsyncException(int flag, Exception ex) {
                super.onAsyncException(flag, ex);
                HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                if (callback != null) {
                    callback.onWanSDKException(ex);
                }
            }

            @Override
            public void onAsyncNetworkException(int flag, Exception ex) {
                super.onAsyncNetworkException(flag, ex);
                HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                if (callback != null) {
                    callback.onWanSDKException(ex);
                }
            }
        });
    }

    public synchronized void initPrivate(){
        init(configuration,initcallback);
    }
    private JuHaoWanSDKCallback initcallback;

    public synchronized void init(JuHaoWanSDKConfiguration configuration, final JuHaoWanSDKCallback callback) {
        initcallback = callback;
        init(configuration);
        boolean agree = HttpEngineApi.getInstance().getEngineConduits().readAgreePrivate();
        if(!agree){
            HttpEngineApi.getInstance().getAgreement(new SimpleAsyncResponseHandler<AgreementResponseParams>(AgreementResponseParams.class) {
                @Override
                public void onAsyncResponseSuccess(int flag, AgreementResponseParams response) {
                    if (response != null && response.isSuccess()) {

//                        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//                        conduits.setPlatformConfigInfo(response.getData());
//                        loadMenu(response.getData().getNav_list());
                        AgreementResponseParams.AgreementData data = response.getData();
                        if(data!=null&&data.getAgreement_popup()!=null&&!TextUtils.isEmpty(data.getAgreement_popup().getUserAgreement())){
                            Intent in=new Intent(getApplicationContent(), PrivateActivity.class);
                            in.putExtra("data",data.getAgreement_popup());
                            in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            configuration.getApplicationContext().startActivity(in);
                        }else{
                            realInit(configuration,callback);
                        }

//                        if (callback != null) {
//                            String message = response.getMsg();
//                            callback.onWanSDKResponse(Constant.InitState.INIT_SUCCESS, message, null);
//                        }
                    } else {
                        HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                        if (callback != null) {
                            String message = response != null ? response.getMsg() : "init failed";
                            callback.onWanSDKResponse(Constant.InitState.INIT_FAILED, message, null);
                        }
                    }
                }

                @Override
                public void onAsyncException(int flag, Exception ex) {
                    super.onAsyncException(flag, ex);
                    HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                    if (callback != null) {
                        callback.onWanSDKException(ex);
                    }
                }

                @Override
                public void onAsyncNetworkException(int flag, Exception ex) {
                    super.onAsyncNetworkException(flag, ex);
                    HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(null);
                    if (callback != null) {
                        callback.onWanSDKException(ex);
                    }
                }
            });

        }else {
            realInit(configuration,callback);
        }


    }

    public synchronized void destroy() {
        HttpEngineApi.getInstance().destroy();

        if (this.configuration != null) {
            this.configuration = null;
            WL.d(TAG, "Destroy WanSDK configuration");
        }

        destroyInstance();
    }

    public String getToutiaoAppid(){
        if (this.configuration != null) {
            String id = this.configuration.getToutiaoId();
            return TextUtils.isEmpty(id)?"0":id;
        }
        return "0";
    }

    public String getGameId() {
        if (this.configuration != null) {
            return this.configuration.getGameId();
        }
        return "0";
    }


    public boolean isShowQuickRegister() {
//        ConfigInfo configInfo = getPlatformConfigInfo();
//        if (configInfo != null) {
//            Integer isOpenCuest = configInfo.isOpenGuest();
//            if (isOpenCuest != null) {
//                return isOpenCuest > 0;
//            }
//        }
//
//        if (this.configuration != null) {
//            return this.configuration.isShowQuickRegister;
//        }
        return true;
    }



    public ConfigInfo getPlatformConfigInfo() {
        return HttpEngineApi.getInstance().getEngineConduits().getPlatformConfigInfo();
    }

    public String getTouTiaoChannel(){
//        String ttChannle = "";
        String channel = HumeSDK.getChannel(applicationContent);
        if ((!TextUtils.isEmpty(channel)) && (!channel.equals("bytedance_hume"))) {
            return channel;
//            SharedPreferences sharedPreferences = JHToolsSDK.getInstance().getApplication().getSharedPreferences("wansdksy", 0);
//            sharedPreferences.edit().putString("tt_channel", channel).commit();
        }else{
            return "";
        }
//        SharedPreferences sharedPreferences = configuration.getApplicationContext().getSharedPreferences("wansdksy", Context.MODE_PRIVATE);
//        if(sharedPreferences.contains("tt_channel")){
//            ttChannle = sharedPreferences.getString("tt_channel","");
//        }
//        return ttChannle;
    }

    private String getExitAppAdvertImageUrl(){
            ExitPopup pop = getPlatformConfigInfo().getGame_exit();
        return pop==null?"":pop.getImageUrl();
    }

    private void checkInitValue() {
        if (this.configuration == null) {
            throw new IllegalArgumentException("WanSDK configuration is not be initialized.");
        }
    }

    public final boolean isLoggedIn() {
        return HttpEngineApi.getInstance().isLoggedIn();
    }

    public void exitApp(Activity activity) {
        String imageUrl = getExitAppAdvertImageUrl();
        String className = XmlUtils.getSdkFactoryClass();
        Object object = ToolsUtils.createInstance(className);
        if (object instanceof IWanSDKFactory) {
            ((IWanSDKFactory) object).showExitAppDialog(activity, imageUrl);
        } else {
            WL.i(TAG, "exitApp ----> object instance is null.");
        }
    }

    public void login(JuHaoWanSDKCallback callback) {
        checkInitValue();
        CallbackObservable.getInstance().setLoginCallbackObserver(callback);
        Intent intent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_LOGIN_ACTIVITY);
        intent.setPackage(configuration.getApplicationContext().getPackageName());
        intent.putExtra(LocalIntentExtra.INTENT_KEY_LOGIN_INIT_POSITION, LocalIntentExtra.INTENT_VALUE_LOGIN_INIT_ACCOUNT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        configuration.getApplicationContext().startActivity(intent);
    }

    public void queryPreventAddictionInfo(JuHaoWanSDKCallback callback) {
        checkInitValue();
        if (callback != null) {
            if (isLoggedIn()) {
                RealNameInfo info = HttpEngineApi.getInstance().getEngineConduits().getRealnameInfo();
                String message = "query success.";
                callback.onWanSDKResponse(Constant.GlobleState.GLOBLE_SUCCESS, message, info.toString());
            } else {
                String message = "The user is not logged in yet.";
                callback.onWanSDKResponse(Constant.GlobleState.GLOBLE_NOT_LOGGED, message, null);
            }
        }
    }

    public void pay(Goods goods, JuHaoWanSDKCallback callback) {
        checkInitValue();
        if (isLoggedIn()) {
            RealNameInfo realNameInfo = HttpEngineApi.getInstance().getEngineConduits().getRealnameInfo();
            if (realNameInfo != null && realNameInfo.isNeedRealNameCertifiesBeforePayment()) {
                Intent newIntent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_LOGIN_ACTIVITY);
                newIntent.setPackage(configuration.getApplicationContext().getPackageName());
                newIntent.putExtra("goods", goods);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                configuration.getApplicationContext().startActivity(newIntent);
            } else {
                CallbackObservable.getInstance().setPayCallbackObserver(callback);
                Intent intent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_PAY_ACTIVITY);
                intent.setPackage(configuration.getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("goods", goods);
                configuration.getApplicationContext().startActivity(intent);

            }
        } else {
            WL.w(TAG, "pay ---> The user is not logged in yet.");
            if (callback != null) {
                String message = "The user is not logged in yet.";
                callback.onWanSDKResponse(Constant.PayState.PAY_INIT_NOT_LOGGED, message, null);
            }
        }
    }

    /**
     * 兼容3.0.6之前的老版本SDK
     * @param roleInfo
     * @param callback
     */
    @Deprecated
    public void uploadRoleInfo(RoleInfo roleInfo, final JuHaoWanSDKCallback callback) {
        uploadRoleInfo(roleInfo, RoleStatus.UNKNOWN, callback);
    }

    public void uploadRoleInfo(RoleInfo roleInfo, RoleStatus status, final JuHaoWanSDKCallback callback) {
        if(roleInfo == null){
            return;
        }

        checkInitValue();
        if (isLoggedIn()) {
            if(isToutiaoInit){
                if(status==RoleStatus.UPGRADE){
                    GameReportHelper.onEventUpdateLevel(Integer.valueOf(roleInfo.getRoleLevel()));
                }
            }
            this.mRoleInfo = roleInfo;
            HttpEngineApi.getInstance().uploadRoleInfo(roleInfo, status, new Callback(callback));
        } else {
            WL.w(TAG, "uploadRoleInfo ---> The user is not logged in yet.");
            if (callback != null) {
                String message = "The user is not logged in yet.";
                callback.onWanSDKResponse(Constant.PayState.PAY_INIT_NOT_LOGGED, message, null);
            }
        }
    }

    public void logout() {
        checkInitValue();
        closeWindow();
        if(this.mRoleInfo != null){
            uploadRoleInfo(this.mRoleInfo.clone(), RoleStatus.LOGOUT, null);
            this.mRoleInfo = null;
        }
        HttpEngineApi.getInstance().logout(null);
        HttpEngineApi.getInstance().getEngineConduits().saveCurrentLoginUserUID("");
    }

    public void openWindowContent() {
        checkInitValue();
        if (isLoggedIn()) {
            WL.d(TAG, "openWindowContent ---> ");
            HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
            if (conduits.isOpenWindowContentView() && hnssnProxyActivity instanceof HNSSNAbsActivity) {
                ((HNSSNAbsActivity) hnssnProxyActivity).openFloatWindowContent();
            }
        } else {
            WL.w(TAG, "openWindow ---> The user is not logged in yet.");
        }
    }

    public void openWindow() {
        checkInitValue();
        if (isLoggedIn()) {
            WL.d(TAG, "openWindow ---> ");
            if (hnssnProxyActivity instanceof HNSSNAbsActivity) {
                ((HNSSNAbsActivity) hnssnProxyActivity).openFloatWindow();
            }
        } else {
            WL.w(TAG, "openWindow ---> The user is not logged in yet.");
        }
    }

    public void closeWindow() {
        checkInitValue();
        if (isLoggedIn()) {
            WL.d(TAG, "closeWindow ---> ");
            if (hnssnProxyActivity instanceof HNSSNAbsActivity) {
                ((HNSSNAbsActivity) hnssnProxyActivity).closeFloatWindow();
            }
        } else {
            WL.w(TAG, "closeWindow ---> The user is not logged in yet.");
        }
    }

    public void startPushWindowMessageTask(RoleInfo roleInfo){
        checkInitValue();
        if (isLoggedIn()) {
            WL.d(TAG, "startPushWindowMessageTask ---> ");

            if (hnssnProxyActivity instanceof HNSSNAbsActivity) {
                ((HNSSNAbsActivity) hnssnProxyActivity).startPushWindowMessageTask(roleInfo);
            }
        } else {
            WL.w(TAG, "startPushWindowMessageTask ---> The user is not logged in yet.");
        }
    }

    @Override
    public void onCreate(Activity activity) {
        this.mActivity = activity;
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onCreate(activity);
        }
    }

    @Override
    public void onStart() {
        WL.w(TAG, "onStart ---> ");
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onStart();
        }
    }

    @Override
    public void onRestart() {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onRestart();
        }
    }

    @Override
    public void onResume() {
        if (isToutiaoInit){
            AppLog.onResume(applicationContent);

        }
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onResume();
        }
    }

    @Override
    public void onPause() {
        if (isToutiaoInit){
            AppLog.onPause(applicationContent);
        }
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onPause();
        }
    }

    @Override
    public void onStop() {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onStop();
        }
    }

    @Override
    public void onDestroy() {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onBackPressed();
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onNewIntent(intent);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
        if (hnssnProxyActivity != null) {
            hnssnProxyActivity.onRequestPermissionResult(requestCode, permissions, grantResults);
        }
    }

    private static final class Callback extends SimpleAsyncResponseHandler<RoleInfoResponseParams> {

        private final JuHaoWanSDKCallback callback;

        Callback(JuHaoWanSDKCallback callback) {
            super(RoleInfoResponseParams.class);
            this.callback = callback;
        }

        @Override
        public void onAsyncResponseSuccess(int flag, RoleInfoResponseParams response) {
            if (callback != null) {
                if (response != null) {
//                    State state = response.getState();
                    callback.onWanSDKResponse(response.getCode(), response.getMsg(), null);
                }
            }
        }

        @Override
        public void onAsyncException(int flag, Exception ex) {
            super.onAsyncException(flag, ex);
            if (callback != null) {
                callback.onWanSDKException(ex);
            }
        }


        @Override
        public void onAsyncNetworkException(int flag, Exception ex) {
            super.onAsyncNetworkException(flag, ex);
            if (callback != null) {
                callback.onWanSDKException(ex);
            }
        }
    }

}
